using System;
using System.Collections;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;
using OracleInternal.ConnectionPool;
using OracleInternal.Network;

namespace OracleInternal.ServiceObjects
{
	internal class NotificationBufferManager : PoolManager<OraclePoolManager, OraclePool, OracleConnectionImpl>
	{
		private static int s_maxListCapacity = 10;

		private static int s_maxOraBufPoolsInBuffer = 5;

		public Hashtable m_htOfOraBufPools = Hashtable.Synchronized(new Hashtable());

		internal object m_htSyncLock = new object();

		internal NotificationBufferManager()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262400, OracleTraceClassName.NotificationBufferManager, OracleTraceFuncName.ctor);
			}
			try
			{
				base.Initialize(new ConnectionString(null, string.Empty, null), null);
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268697600, OracleTraceClassName.NotificationBufferManager, OracleTraceFuncName.ctor, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262656, OracleTraceClassName.NotificationBufferManager, OracleTraceFuncName.ctor);
				}
			}
		}

		public void GetNotificationOraBufPool(OracleCommunication orclCommunication)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262400, OracleTraceClassName.NotificationBufferManager, OracleTraceFuncName.GetNotificationOraBufPool);
			}
			try
			{
				SyncQueueList<ConOraBufPool> syncQueueList = (SyncQueueList<ConOraBufPool>)m_htOfOraBufPools[orclCommunication.SDU];
				if (syncQueueList == null || !syncQueueList.Dequeue(out var t))
				{
					int num = m_cs.m_maxPoolSize;
					if (num > 200)
					{
						num = 200;
					}
					t = new ConOraBufPool(new OraBufPool(num * s_maxListCapacity));
					t.Init(orclCommunication);
				}
				orclCommunication.OraBufPool = t;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268697600, OracleTraceClassName.NotificationBufferManager, OracleTraceFuncName.GetNotificationOraBufPool, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262656, OracleTraceClassName.NotificationBufferManager, OracleTraceFuncName.GetNotificationOraBufPool);
				}
			}
		}

		public void PutNotificationOraBufPool(OracleCommunication orclCommunication)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262400, OracleTraceClassName.NotificationBufferManager, OracleTraceFuncName.PutNotificationOraBufPool);
			}
			try
			{
				_ = orclCommunication.OraBufPool;
				orclCommunication.OraBufPool.Init(orclCommunication);
				SyncQueueList<ConOraBufPool> syncQueueList = (SyncQueueList<ConOraBufPool>)m_htOfOraBufPools[orclCommunication.SDU];
				if (syncQueueList == null)
				{
					lock (m_htSyncLock)
					{
						syncQueueList = (SyncQueueList<ConOraBufPool>)m_htOfOraBufPools[orclCommunication.SDU];
						if (syncQueueList == null)
						{
							syncQueueList = new SyncQueueList<ConOraBufPool>();
							m_htOfOraBufPools.Add(orclCommunication.SDU, syncQueueList);
						}
					}
				}
				if (syncQueueList.Count < s_maxOraBufPoolsInBuffer)
				{
					syncQueueList.Enqueue(orclCommunication.OraBufPool);
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268697600, OracleTraceClassName.NotificationBufferManager, OracleTraceFuncName.PutNotificationOraBufPool, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262656, OracleTraceClassName.NotificationBufferManager, OracleTraceFuncName.PutNotificationOraBufPool);
				}
			}
		}
	}
}
